package com.eastcom.collection.process

import java.util.List;

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.codehaus.groovy.tools.shell.Shell
import org.springframework.beans.factory.annotation.Value
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.springframework.stereotype.Component

import com.eastcom.collection.tools.file.FileUtils
import com.eastcom.collection.tools.file.JaxbUtil;
import com.eastcom.collection.context.*
import com.eastcom.collection.provider.*
import com.eastcom.collection.tools.db.DbEngine
import com.eastcom.collection.tools.rmi.ift.ITaskSender;
import com.eastcom.collection.timingtask.TaskManager;

import javax.annotation.Resource

/**
 * Created by yuanyue on 2014-08-03.
 */
@Component("taskInitProcess")
class TaskInitProcess {
	private static Log logger = LogFactory.getLog(TaskInitProcess.class)
	@Resource(name = "threadPool")
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	@Resource(name = "dbEngine")
	private DbEngine dbEngine;
	@Resource(name = "groovyTaskManager")
	private TaskManager taskManager;
	/**
	 *
	 */
	public void init(final List<String> taskConfigPath){

		threadPoolTaskExecutor.submit(new Runnable() {
					@Override
					public void run() {
						RmiServiceProvider rmi = SpringContextProvider.getBean("rmiProvider")
						ITaskSender tastkSender = SpringContextProvider.getBean("taskSender")
						rmi.setResource(tastkSender)
						rmi.setRmiServiceAddr(PropertyPlaceholderConfigurerProvider.getContextProperty("rmiTaskSenderAddr"))
						rmi.setRmiServiceName(PropertyPlaceholderConfigurerProvider.getContextProperty("rmiTaskSenderName"))
						rmi.setRmiServicePort(Integer.valueOf(PropertyPlaceholderConfigurerProvider.getContextProperty("rmiTaskSenderPort")))
						rmi.start()
					}
				});
		threadPoolTaskExecutor.submit(new Runnable() {
					@Override
					public void run() {
						taskManager.loadTask(taskConfigPath)
					}
				});
		threadPoolTaskExecutor.submit(new Runnable() {
					@Override
					public void run() {
						DataCacheProcess dataCacheProcess = SpringContextProvider.getBean("dataCacheProcess")
						dataCacheProcess.start()
					}
				});
	}
}

